#!/bin/bash
# Cleanup version 3

# Warning:
# a number of features that will be explained later on.

LOG_DIR=/var/log
ROOT_UID=0
LINES=50
E_XCD=86
E_NOTROOT=87
ORIG_DIR=`pwd`
#echo $ORIG_DIR
#echo $UID
#echo $ROOT_UID

# Run as root, of course.

#if [ "$UID" -ne "$ROOT_UID" ]
#then
#    echo "Must be run as root"
#    exit $E_NOTROOT
#fi
#

#if [ -n "$1" ]
#then
#    lines=$1
#else
#    lines=$LINES
#fi
#

# A better way of checking command-line arguments
E_WRONGARGS=85

case "$1" in
    "" ) lines=50;;
    *[!0-9]*) echo "Usage: `basename $0` file-to-cleanup"; exit $E_WRONGARGS;;
    *   ) lines=$1;;
esac

cd $LOG_DIR
if [ `pwd` != "$LOG_DIR" ]
then
    echo "Can't change to $LOG_DIR."
    exit $E_XCD
fi

tail -n $lines messages > $ORIG_DIR/msg.temp

echo "Logs cleaned up."
exit 0
